package com.xindaoapp.happypet.ui;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.AtMessageCenterAdapter;
import com.xindaoapp.happypet.adapter.AtMessageCenterAdapter.ViewHolder;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.MessageCenterInfo;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * @ 我的列表
 * 
 * @author Stone
 * 
 */
public class MyAtInfoListActivity extends BaseActivity {

    private PullToRefreshListView mPullToRefreshListView;
    private AtMessageCenterAdapter mAdapter;
    private RelativeLayout mNoData;
    private Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_my_at_info_list;
    }

    @Override
    protected void initViews() {
        mContext = this;

        mNoData = (RelativeLayout) this.findViewById(R.id.no_data);

        mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
        mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                onLoadDatas();
            }
        });
        mPullToRefreshListView.getRefreshableView().setOnItemClickListener(
                new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long arg3) {

                        MessageCenterInfo info =
                                (MessageCenterInfo) parent.getAdapter().getItem(position);
                        if (info != null && view.getTag() != null) {
                            handleOnClick(info, ((ViewHolder) view.getTag()).isNew,
                                    ((ViewHolder) view.getTag()).count);
                        }

                    }
                });

        mPullToRefreshListView.getRefreshableView().setOnItemLongClickListener(
                new OnItemLongClickListener() {

                    @Override
                    public boolean onItemLongClick(final AdapterView<?> parent, View arg1,
                            final int position, long arg3) {

                        new AlertDialog.Builder(mContext).setTitle(" @我的").setMessage("确认删除？")
                                .setNegativeButton("取消", null)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();
                                        MessageCenterInfo info =
                                                (MessageCenterInfo) parent.getAdapter().getItem(
                                                        position);
                                        String id = info.id;
                                        delete(position, id);
                                    }
                                }).create().show();
                        return false;


                    }
                });

        super.initViews();
    }

    private void delete(final int position, String noticeId) {
        getMoccaApi().deleteInformMessage(noticeId, new IRequest<Pair<Boolean, String>>() {

            @Override
            public void request(Pair<Boolean, String> t) {
                if (null != t) {
                    if (t.first) {
                        mAdapter.getResult().remove(position - 1);
                        mAdapter.notifyDataSetChanged();
                    }
                    Toast.makeText(MyAtInfoListActivity.this, t.second, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MyAtInfoListActivity.this, R.string.net_error,
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    protected void handleOnClick(MessageCenterInfo info, View isNew, TextView count) {

        // 跳转到PostDetailActivity
        Intent intent = new Intent(this, PostDetailActivity.class);
        intent.putExtra(Constants.KEY_POST_TID, info.tid);
        // intent.putExtra(Constants.KEY_PKUID, info.authorid);

        startActivity(intent);


        if (info.isnew == 1) {
            info.isnew = 0;
            count.setText("");
            isNew.setVisibility(View.GONE);
            HappyPetApplication.get().getMoccaApi().noticeClick(info.id, new IRequest<Boolean>() {

                @Override
                public void request(Boolean t) {
                    if (t) {
                        Constants.NoticeLabel.sHasNewMessageNotice = true;
                    } else {
                        Constants.NoticeLabel.sHasNewMessageNotice = false;
                    }
                    Intent notice = new Intent(Constants.KEY_NOTICE_STATE_CHANGED);
                    sendBroadcast(notice);
                }
            });
        }


    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();


        getData();


    }



    private void getData() {



        getMoccaApi().getAtMessageCenterInfos(Constants.PAGE_FIRST, Constants.PAGE_SIZE,
                new IRequest<List<MessageCenterInfo>>() {

                    @Override
                    public void request(List<MessageCenterInfo> t) {
                        if (t != null) {
                            if (t.size() == 0) {
                                Toast.makeText(getApplicationContext(), "暂时还没有人 @你!",
                                        Toast.LENGTH_SHORT).show();
                                mNoData.setVisibility(View.VISIBLE);
                            } else {
                                onDataArrived(true);

                                mNoData.setVisibility(View.GONE);
                                mAdapter =
                                        new AtMessageCenterAdapter(MyAtInfoListActivity.this, t,
                                                Constants.PAGE_SIZE,
                                                R.layout.item_message_center_at,
                                                R.layout.item_loading);
                                mPullToRefreshListView.getRefreshableView().setAdapter(mAdapter);
                            }

                        } else {
                            onDataArrived(false);
                        }
                        mPullToRefreshListView.onRefreshComplete();
                    }
                });


    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }


    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }


    @Override
    protected String getTopBarTitleStrRes() {
        return "@我的";
    }

}
